package com.watermoniter.util;

import android.content.Context;
import android.net.http.SslError;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.watermoniter.BuildConfig;

/**
 * 作者：minj on 2017/9/29 16:43
 * webview管理类
 */
public class WebViewUtil {



    public static void initWebView(final Context context, final WebView webView) {

        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
                    webView.goBack();
                    return true;
                }
                return false;
            }
        });


        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setDomStorageEnabled(true);
        webSettings.setSupportZoom(false);
//
        if(!AppConfig.isNetworkAvailable(context)){
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }else {
            if(BuildConfig.DEBUG){
                webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            }else {
                webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
            }

        }



        webSettings.setAppCacheEnabled(true);

        webView.setWebViewClient(new WebViewClient() { //禁止超链接


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();// 接受所有网站的证书
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);
//                context.showToast(R.string.no_network);
//                webView.setVisibility(View.GONE);

            }
        });

        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
//        webView.setInitialScale(60);
    }


    /***
     * 通过文件名获得文件的mime类型
     * @param url
     * @return
     */
    private static String getMimiType(String url){
        if (url.endsWith(".png")) {
            return "image/png";
        } else if (url.endsWith(".gif")) {
            return "image/gif";
        } else if (url.endsWith(".jpg")) {
            return "image/gif";
        } else if (url.endsWith(".jepg")) {
            return "image/jepg";
        } else if (url.endsWith(".js") ) {
            return "text/javascript";
        } else if (url.endsWith(".css") ) {
            return "text/css";
        } else if (url.endsWith(".html")) {
            return "text/html";
        }

        return "";

    }



}
